import {Component} from '@angular/core';
import {App, IonicPage, NavController} from 'ionic-angular';
import {PublicService} from "../../common/public.service";
import {CAPACITY_REPORT_URL} from "../../providers/Constants";
import {HttpService} from "../../providers/HttpService";
import {DatePicker} from "@ionic-native/date-picker";

/**
 * 人均产能
 * 班长可以登录手持端查看自己工序以及此工序中各工站的劳效。
 */

@IonicPage()
@Component({
  selector: 'page-capacity-report',
  templateUrl: 'capacity-report.html',
})
export class CapacityReportPage {

  bzDate: string;//班组日期
  bzDate_old: string;//监控日期是否变化
  bzSelect: string;//班组选择
  procedureId: string;//工序id
  procedureName: string;//工序

  dataListLen: number;

  //处理类型
  public workProcedures: Array<Object> = JSON.parse(localStorage.getItem("mpower-workProcedure"));

  //扫描后把数据保存在这里
  public dataList: Array<Object> = [];

  constructor(public navCtrl: NavController,
              public http: HttpService,
              public app: App,
              public datePicker: DatePicker,
              public pub: PublicService) {
  }

  ionViewDidLoad() {
    this.bzDate = sessionStorage.getItem("setWorkTime");
    if (!this.bzDate) {
      this.bzDate = this.pub.getDate(new Date(), 0);
    }
    this.bzSelect = sessionStorage.getItem("setBzSelect");
    this.procedureId = sessionStorage.getItem("workProcedure");
    this.procedureName = sessionStorage.getItem("workProcedureName");
    this.bzDate_old = this.bzDate;
  }

  ionViewWillEnter() {
    this.initList();
  }


  /**
   * 查询日期
   */
  getDate() {
    this.datePicker.show({
      titleText: "",
      date: this.bzDate,
      mode: 'date',
      maxDate: new Date(),
      is24Hour: true,
      androidTheme: this.datePicker.ANDROID_THEMES.THEME_HOLO_LIGHT
    }).then(
      date => {
        this.bzDate = this.pub.getDate(date, 0);
        if (this.bzDate != this.bzDate_old) {
          this.bzDate_old = this.bzDate;
          this.changeQuery();
        }
      },
      err => {
        console.log('Error occurred while getting date: ', err);
      }
    );
  }


  /**
   * 初始化列表
   */
  initList() {
    this.dataList = [];
    let params = {
      sessionId: sessionStorage.getItem("session"),
      groupDate: this.bzDate,
      groupType: this.bzSelect,
      procedureId: this.procedureId
    };
    this.http.get(SERVER_URL + CAPACITY_REPORT_URL, '&', params, false).then(res => {
      if (res.success) {
        this.dataList = res.data.stationCapacity;
        this.dataListLen = this.dataList.length + 1;
        if (res.data.statisInfo) {
          this.dataList.push({
            "duration": res.data.statisInfo.allDur,
            "capacity": res.data.statisInfo.allCap,
            "stationName": "合计",
            "perCapacity": res.data.statisInfo.allPerDur
          })
        }

        let html = '';
        if (this.dataList.length > 0) {
          for (let i = 0; i < this.dataList.length; i++) {
            html += `<tr>`;
            if (i == 0) {
              html += `<td rowspan="` + this.dataListLen + `">` + this.procedureName + `</td>`;
            }
            html += `<td>` + this.dataList[i]['stationName'] + `</td>`;
            html += `<td>` + this.dataList[i]['capacity'] + `</td>`;
            html += `<td>` + this.dataList[i]['duration'] + `</td>`;
            html += `<td>` + this.dataList[i]['perCapacity'] + `</td>`;
            html += `</tr>`;
          }
        }

        document.getElementById("capacity_table_body").innerHTML = html;

      } else {
        this.pub.presentLoadingToast(res.msg);
        if (res.timeout) {
          setTimeout(() => {
            this.app.getRootNav().setRoot('LoginPage');
          }, 1500);
        }
      }
    });
  }


  /**
   *
   * 切换查询条件
   */
  changeQuery() {
    this.initList();
  }
}
